<?php
namespace app\shops\controller;

use think\Db;
use think\Model;

/**
 * 课程管理
 */
class Course extends Base
{
    /**
     * @throws
     * 课程列表
     * @return [type] [description]
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();
        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 获取前台传过来的查询条件
        $course_type = input('get.keywords') ? : "";
        $this->assign('keywords',$course_type);
        $pageParam['query']['course_type'] = $course_type;

        // 列表显示所有课程
        $list = Db("course")->alias('c')->join('store sr', 'c.store_id=sr.store_id')
            ->field('c.id as course_id, c.course_name, c.status, c.is_arrange, c.course_type, sr.store_name, sr.store_id')
            ->where('c.shop_id',$this->shop_id)
            ->where('c.course_type','like',"%{$course_type}%")
            ->order("c.id asc")
            ->paginate(['query'=>request()->param(),$pageSize]);

        $this->assign([
            'total'     => $list->total(),
            'page'      => $list->render(),
            'list'      => $list
        ]);
        return $this->fetch();

    }

    /**
     * 添加课程
     */
    public function add()
    {

        // 新增课程信息
        if(request()->isPost()){

            $data = [
                'shop_id'           => $this->shop_id,
                'store_id'          =>input('store_id'),
                'course_name'       =>input('course_name'),
                'course_type'       =>input('course_type'),
            ];
            $re = Db("course") -> insert($data);

            if($re){
                $this->success("添加成功");
            }else{
                $this->error("网络错误");
            }
        }

        // 获取门店列表
        $store_list = Db('store')->where('shop_id',$this->shop_id)->field('store_id,store_name')->select();
        $this->assign('store_list', $store_list);

        return $this->fetch();
    }

    /**
     * @throws
     * 编辑课程信息
     */
    public function edit()
    {
        $course_id = input('course_id');

        // 更新数据
        if(request()->isAjax()){

            input('store_id')       == null || $data['store_id']      = input('store_id');
            input('course_id')      == null || $data['id']            = input('course_id');
            input('course_name')    == null || $data['course_name']   = input('course_name');
            input('status')         == null || $data['status']        = input('status');


            $res = Db("course")->where('shop_id',$this->shop_id)->where('id',$data['id'])->update($data);
            if($res===false){
                $this->error("编辑失败");
            }
            $this->success("编辑成功");

        }

        //拿到该课程相关的信息
        $course_info = Db("course")->alias('c')->join('store s','c.store_id=s.store_id')
            ->field('c.id as course_id, c.course_name, c.status, c.course_type, s.store_id, s.store_name')
            ->where('c.shop_id',$this->shop_id)->where('c.id',$course_id)->find();

        $this->assign('course_info',$course_info);

        return $this->fetch();
    }

    /**
     *课程的状态（开课/闭课）
     */
    public function is_show(){

        if (request()->isAjax()){
            // 获取参数
            $course_id    = input('course_id');
            $status       = input('status');

            // 修改课程状态
            $res = Db('course')->where('shop_id', $this->shop_id)->where('id', $course_id)->update(['status' => $status]);

            // 返回结果
            if($res){
                $this->success("成功");
            }else{
                $this->error("网络异常");
            }
        }

    }

}
